package com.mxprojects.io
{
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.text.TextField;
	

	public class TagParser
	{
		private static const TAGS:Array = new Array('<',    '>',    '[b]', '[/b]', '[pre]', '[/pre]', '[i]', '[/i]', '[u]', '[/u]', '\r',   '\n',   '&amp;', 'www.',        'http://http://', 'ftp://http://', 'https://http://');
		private static const HTML:Array = new Array('&lt;', '&gt;', '<b>', '</b>', '<pre>', '</pre>', '<i>', '</i>', '<u>', '</u>', '<br>', '<br>', '&',     'http://www.', 'http://',        'ftp://',        'https://');
		private static const PROTOCOLS:String = "http|ftp|https";
		private static const TEXT_TAGS:Array = new Array(   '&lt;', '&gt;', '&quot;', '&amp;');
		private static const TEXT_REPLACE:Array = new Array('<',    '>',    '\"',     '&');
		private static const START_LINK:String = "<span class=\"tagParserLink\">";
		private static const END_LINK:String = "</span>";
		
		
		
		public static function convertToHtml(text:String):String {
			//take text filled with tags and convert those tags to HTML
			var html:String = text;
			
			//find all the tags case in-sensative
			for(var i:int = 0; i < TAGS.length; i++){
				var pattern:RegExp = new RegExp(Utils.escapeRegEx(TAGS[i]), "gi");
				html = html.replace(pattern, HTML[i]);
			}
			
			//see if there are any URLs
			html = checkURL(html);
			
			//see if there are any email tags or addresses
			html = checkEmail(html);
			
			//check for any quoted text
			html = checkQuote(html);
			
			return html;
		}
		
		public static function convertToText(string:String):String {
			//basically take some special HTML chars and make them readable
			var text:String = string;
			
			//find all the tags case in-sensative
			for(var i:int = 0; i < TEXT_TAGS.length; i++){
				var pattern:RegExp = new RegExp(Utils.escapeRegEx(TEXT_TAGS[i]), "gi");
				text = text.replace(pattern, TEXT_REPLACE[i]);
			}
			
			return text;
		}
		
		public static function parseQuote(textField:TextField):void {
			//match [quote]  [quote=xxxxx]
			/*
			var quote:String = textField.text;
			
			//handle regular [quote] links
			var regularQuote:RegExp = new RegExp("\\[quote\\](.*?)\\[/quote\\]", "gi");
			
			var matches:Array = quote.match(regularQuote);
			var matchesLength:int = matches.length;
			var startIndex:int = 0;
			
			for(var i:int = 0; i < matchesLength; i++){
				var quoteStart:int = quote.indexOf(matches[i], startIndex);
				var firstChar:Rectangle = textField.getCharBoundaries(quoteStart);
				var postQuoteBox:PostQuoteBox = new PostQuoteBox();
				postQuoteBox.parse(textField, firstChar);
			}
			
			
			quote = quote.replace(regularQuote, "<span class=\"quoteIntro\">" + Language.load(Word.QUOTE) + "</span><br><span class=\"quoteBody\">$1</span><br>");	
			
			//handle [quote=XXXXX] links
			//var specialQuote:RegExp = new RegExp("\\[quote=([^\\]]+)\\](.*?)\\[/quote\\]", "gi");
			//quote = quote.replace(specialQuote, Language.load(Word.QUOTING) + " $1: <br><br> $2");
			
			//return quote;
			*/
		}
		
		private static function checkURL(text:String):String {
			//match up [url]  [url=xxxxx]
			var url:String = text;

			//handle regular [url] links
			var regularURL:RegExp = new RegExp("\\[url\\]([a-zA-Z0-9/%@?:#&+._=-]*)\\[/url\\]", "gi");
			url = url.replace(regularURL, START_LINK + "<a href=\"$1\" target=\"_blank\">$1</a>" + END_LINK);
			
			//handle links that have "[url=????]"
			var specialURL:RegExp = new RegExp("\\[url=(("+PROTOCOLS+")://){1}([a-zA-Z0-9/%@?:#&+._=-]*)\\](.*?)\\[/url\\]", "gi");
			url = url.replace(specialURL, START_LINK + "<a href=\"$1$3\" target=\"_blank\">$4</a>" + END_LINK);			
			
			//handle URLs that don't have tags
			var inlineURL:RegExp = new RegExp("("+PROTOCOLS+")://[a-zA-Z0-9/%@?#&+._=-]*", "gi");	
			var matches:Array = url.match(inlineURL);
			var matchesLength:int = matches.length;
			var startIndex:int = 0;
			
			//loop through the array and replace the HTML to what it should be
			for(var i:int = 0; i < matchesLength; i++){
				var urlStart:int = url.indexOf(matches[i], startIndex);
				
				//look before the URL to see if it needs to be parsed
				var prevText:String = url.substring(urlStart-8, urlStart);
				
				if(prevText != "a href=\"" && prevText != "_blank\">"){
					//parse it!
					var firstHalf:String = url.substring(0, urlStart);
					var secondHalf:String = url.substring(urlStart + matches[i].length);
					
					//display the new text
					firstHalf += START_LINK + "<a href=\"" + matches[i] + "\" target=\"_blank\">" + matches[i] + "</a>" + END_LINK;
					url = firstHalf + secondHalf;
				}
				
				//push the start index forward through the string
				startIndex = urlStart + matches[i].length;
			}
			
			return url;
		}
		
		private static function checkEmail(text:String):String {
			//match up email@whatever.com
			var email:String = text;

			//handle regular email
			var inlineEmail:RegExp = new RegExp("([A-Z0-9._%+-]+@[A-Z0-9._%+-]+)", "gi");
			email = email.replace(inlineEmail, START_LINK + "<a href=\"mailto:$1\">$1</a>" + END_LINK);
			
			return email;
		}
		
		private static function checkQuote(text:String):String {
			//match [quote]  [quote=xxxxx]
			var quote:String = text;
			
			//handle regular [quote] links
			var regularQuote:RegExp = new RegExp("\\[quote\\](.*?)\\[/quote\\]", "gi");			
			quote = quote.replace(regularQuote, "<span class=\"quoteIntro\">" + Language.main(Word.QUOTE) + "</span><br><span class=\"quoteBody\">$1</span>");	
			
			//handle [quote=XXXXX] links
			var specialQuote:RegExp = new RegExp("\\[quote=([^\\]]+)\\](.*?)\\[/quote\\]", "gi");
			quote = quote.replace(specialQuote, "<span class=\"quoteIntro\">" + Language.main(Word.QUOTING) + " $1:</span><br><span class=\"quoteBody\">$2</span>");
			
			return quote;
		}
	}
}